/**
 *
 */
package com.bsoft.gol.hcb.dao.evaluation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import pcn.sign.BaseEvaluationDefine;

import java.util.ArrayList;
import java.util.List;

/**
 * @since 2017-05-02 11:36:36
 */
@SsdevDao(serviceId = "baseEvaluationDefineDAOService")
public abstract class BaseEvaluationDefineDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<BaseEvaluationDefine> {

	public BaseEvaluationDefineDAO() {
		this.setKeyField("id");
	}

	@DAOMethod(sql = "from BaseEvaluationDefine where id=:id")
	public abstract BaseEvaluationDefine getBaseEvaluationDefineById(@DAOParam("id") int id);

	@DAOMethod(sql = "from BaseEvaluationDefine where id=:id and status='1'")
	public abstract BaseEvaluationDefine getBaseEvaluationDefineByIdAndStatus(@DAOParam("id") int id);

	@DAOMethod(sql = "select count(1) from BaseEvaluationDefine where businessName=:businessName")
	public abstract Long getBaseEvaluationDefineByBusinessName(@DAOParam("businessName") String businessNam);

//	public List<BaseEvaluationDefine> getFirstEvaluationList(final Integer isEnable) {
	public List<BaseEvaluationDefine> getFirstEvaluationList(final String isEnable) {
		HibernateStatelessResultAction<List<BaseEvaluationDefine>> action =
                new AbstractHibernateStatelessResultAction<List<BaseEvaluationDefine>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuilder hsql = new StringBuilder();
				hsql.append("from BaseEvaluationDefine where priorLevel=1 ");
				if (isEnable != null) {
					hsql.append("and status=:isEnable");
				}
				Query query = ss.createQuery(hsql.toString());
				if (isEnable != null) {
					query.setParameter("isEnable", isEnable);
				}

				setResult(query.list());
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}


	@DAOMethod(sql = "from BaseEvaluationDefine where priorLevel=:priorLevel")
	public abstract List<BaseEvaluationDefine> queryBaseEvaluationDefineByPriorLevel(@DAOParam("priorLevel") Byte priorLevel);


	@DAOMethod(sql = "from BaseEvaluationDefine where businessType=:businessType and priorLevel=:priorLevel")
	public abstract BaseEvaluationDefine getBaseEvaluationDefineByBussinessType(@DAOParam("businessType") String businessType, @DAOParam("priorLevel") Byte priorLevel);
    @RpcService
	@DAOMethod(sql = "from BaseEvaluationDefine where businessType=:businessType and priorLevel=:priorLevel and " +
            "status='1'")
	public abstract BaseEvaluationDefine getBaseEvaluationDefineByBussinessTypeAndStatus(@DAOParam("businessType") String businessType, @DAOParam("priorLevel") Byte priorLevel);

	public BaseEvaluationDefine getLast() {
		HibernateStatelessResultAction<BaseEvaluationDefine> action =
                new AbstractHibernateStatelessResultAction<BaseEvaluationDefine>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer hsql = new StringBuffer();
				hsql.append("select * from base_evaluation_define where prior_level=1 order by id desc limit 1");
				Query query = ss.createSQLQuery(hsql.toString());

				query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
				List listi = query.list();
				if (listi.size() == 0) {
					setResult(null);
				} else {

					List<BaseEvaluationDefine> list = new ArrayList<>();
					for (Object o : listi) {
						list.add(ConversionUtils.convert(o, BaseEvaluationDefine.class));
					}

					setResult(list.get(0));
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}


	@DAOMethod(sql = "from BaseEvaluationDefine")
	public abstract List<BaseEvaluationDefine> findBaseEvaluationDefines();

	@DAOMethod(sql = " update  BaseEvaluationDefine set businessName=:businessName  where id=:id")
	public abstract void updateBussinessName(@DAOParam("businessName") String businessName, @DAOParam("id") int id);


	public void updateEvaluationDefaine(final BaseEvaluationDefine baseEvaluationDefine) {
		HibernateStatelessResultAction action = new AbstractHibernateStatelessResultAction() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				Query query = null;

				query = ss.createQuery("update BaseEvaluationDefine t set t.addEvaluationFlag=:addEvaluationFlag,t" +
                        ".contentFlag=:contentFlag,t.status=:status,t.businessName=:businessName where t.id=:id");
				query.setParameter("id", baseEvaluationDefine.getId());
				query.setParameter("addEvaluationFlag", baseEvaluationDefine.getAddEvaluationFlag());
				query.setParameter("contentFlag", baseEvaluationDefine.getContentFlag());
				query.setParameter("status", baseEvaluationDefine.getStatus());
				query.setParameter("businessName", baseEvaluationDefine.getBusinessName());
				query.executeUpdate();
			}
		};
		HibernateSessionTemplate.instance().execute(action);
	}

}